import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {

  // 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀。
  const env = loadEnv(mode, process.cwd(), '')
  console.log(JSON.stringify(env.VITE_APP_MODE))
  return {
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    // vite 配置
    define: {
      // 环境状态
      __APP_ENV__: JSON.stringify(env.VITE_APP_MODE),
    },
    build: {
      minify: "terser", // 必须开启：使用terserOptions才有效果
      terserOptions: {
        compress: {
          //生产环境时移除console
          drop_console: true,
          drop_debugger: true,
        },
      },
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    server: {
      cors: true,
      proxy: {
        "^/api": {
          target: env.VITE_APP_BASE_URL, // 真实接口地址, 后端给的基地址
          changeOrigin: true, // 允许跨域
          rewrite: (path) => path.replace(/^\/api/, "/api"),
        },
      },
    }
  }
})
